/*
 * Copyright 2013 Aliyun.com All right reserved. This software is the
 * confidential and proprietary information of Aliyun.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Aliyun.com .
 */
package com.mtauto.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;

/**
 * @author <a href="mailto:li.jinl@alibaba-inc.com">Stone.J</a> Nov 25, 2013
 */
public class View implements Serializable {

    private static final long serialVersionUID = 1L;

    private String            type;
    private String            pkg;
    private String            text;
    private int               index;
    private Point             p1;
    private Point             p2;

    private View              parent;
    private Collection<View>  children         = new ArrayList<View>();

    public View() {
    }

    public boolean isRoot() {
        return this.parent == null;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getPkg() {
        return pkg;
    }

    public void setPkg(String pkg) {
        this.pkg = pkg;
    }

    public String getText() {
        return text;
    }

    public void setText(String text) {
        this.text = text;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public View getParent() {
        return parent;
    }

    public void setParent(View parent) {
        this.parent = parent;
    }

    public Collection<View> getChildren() {
        return children;
    }

    public void setChildren(Collection<View> children) {
        this.children = children;
    }

    public Point getP1() {
        return p1;
    }

    public void setP1(Point p1) {
        this.p1 = p1;
    }

    public Point getP2() {
        return p2;
    }

    public void setP2(Point p2) {
        this.p2 = p2;
    }

    public String dump() {
        StringBuilder sb = new StringBuilder("Android View Dump: \n");
        dump(this, sb, 0);
        return sb.toString();
    }

    private void dump(View view, StringBuilder sb, int level) {
        for (int i = 0; i < level; i++) {
            sb.append("    ");
        }
        sb.append(view.getType()).append(" ").append(view.getIndex()).append(" ").append(view.getText());
        sb.append("\n");
        for (View v : view.getChildren()) {
            dump(v, sb, ++level);
            --level;
        }
    }

    public static class Point implements Serializable {

        private static final long serialVersionUID = 1L;

        private int               x;
        private int               y;

        public Point(int x, int y) {
            this.x = x;
            this.y = y;
        }

        public int getX() {
            return x;
        }

        public void setX(int x) {
            this.x = x;
        }

        public int getY() {
            return y;
        }

        public void setY(int y) {
            this.y = y;
        }

    }

}
